package com.logistics.Action;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.DAO.Action;
import com.DAO.User;
import com.opensymphony.xwork2.ActionSupport;

public class Adder extends ActionSupport implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	/**
	 * @title Manager.java
	 * @package com.logistics.Action
	 * @author ufo
	 */

	private Map<String,String> result=new HashMap<String,String>();
	private HttpServletRequest request;
	private String[] id;
	private String[] tid;
	private String[] description;
	private String[] group;
	private String[] startDate;//形式均为yyyy-mm-dd hh:mm:ss
	private String[] checkDate;
	private String[] endDate;
	private String[] score;

	protected Configuration conf=new Configuration().configure();
	protected SessionFactory sf=conf.buildSessionFactory();
	protected Session hibernateSess=sf.openSession();
	protected Transaction tx = hibernateSess.beginTransaction();
	protected User user;
	protected Action action;

	public String alt(){
		if(isOnline()){
			addSessionToUser();
			if(user.getFlag()!=1){
				result.put("flag", "false");
				return SUCCESS;
			}
			action=(Action) hibernateSess.load(Action.class, getId()[0]);
			action.setCheckDate(getCheckDate()[0]);
			action.setDescription(getDescription()[0]);
			action.setEndDate(getEndDate()[0]);
			action.setGroup(getGroup()[0]);
			action.setScore(Integer.parseInt(getScore()[0]));
			action.setStartDate(getStartDate()[0]);
			action.setTid(getTid()[0]);
			hibernateSess.save(action);
			tx.commit();
			result.put("flag","true");
		}
		result.put("flag", "false");
		return SUCCESS;
	}
	public String add(){
		if(isOnline()
				&&getGroup()[0]!=null
				&&getStartDate()[0]!=null
				&&getCheckDate()[0]!=null
				&&getEndDate()[0]!=null)
		{
			addSessionToUser();
			if(user.getFlag()==1){
				action=new Action();
				action.setCheckDate(getCheckDate()[0]);
				action.setDescription(getDescription()[0]);
				action.setEndDate(getEndDate()[0]);
				action.setGroup(getGroup()[0]);
				action.setStartDate(getStartDate()[0]);
				action.setTid((String) request.getSession().getAttribute("userId"));
				action.setScore(Integer.parseInt(getScore()[0]));
				hibernateSess.save(action);
				tx.commit();
				result.put("flag", "true");
				return SUCCESS;
			}
		}
		result.put("flag", "false");
		return SUCCESS;
	}
	//下面这个函数我看不出来有什么存在的必要啊……
	public Action add(String userId){
		if(isOnline()
				&&getGroup()[0]!=null
				&&getStartDate()[0]!=null
				&&getCheckDate()[0]!=null
				&&getEndDate()[0]!=null)
		{
			action=new Action();
			action.setDescription(userId);
			action.setGroup("0");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			action.setStartDate(df.format(new Date()));
			
			Calendar cal = Calendar.getInstance();
			String[] endDate=df.format(new Date()).split("-");
			if(cal.MONTH+1<=6)
				endDate[1]="7".toString();
			else{
				endDate[0]=String.valueOf(cal.YEAR);
				endDate[1]="3".toString();
			}

			action.setCheckDate(df.format(new Date()));
			action.setEndDate(endDate[0]+"-"+endDate[1]+"-"+endDate[2]);
			action.setTid("999999999");
			hibernateSess.save(action);
			tx.commit();
			result.put("flag", "true");
			return action;
		}
		return null;
	}
	public boolean isOnline(){
		if(request.getSession().getAttribute("userId")!=null)
			return true;
		else 
			return false;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO 自动生成的方法存根
		this.setRequest(request);
	}
	@JSON(name="result")
	public Map<String,String> getResult() {
		return result;
	}
	public void setResult(Map<String,String> result) {
		this.result = result;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public String[] getDescription() {
		return description;
	}
	public void setDescription(String[] description) {
		this.description = description;
	}
	public String[] getGroup() {
		return group;
	}
	public void setGroup(String[] group) {
		this.group = group;
	}
	public String[] getStartDate() {
		return startDate;
	}
	public void setStartDate(String[] startDate) {
		this.startDate = startDate;
	}
	public String[] getCheckDate() {
		return checkDate;
	}
	public void setCheckDate(String[] checkDate) {
		this.checkDate = checkDate;
	}
	public String[] getEndDate() {
		return endDate;
	}
	public void setEndDate(String[] endDate) {
		this.endDate = endDate;
	}
	private void addSessionToUser(){
		user=(User) hibernateSess.load(User.class, (Serializable) request.getSession().getAttribute("userId"));
	}
	public String[] getId() {
		return id;
	}
	public void setId(String[] id) {
		this.id = id;
	}
	public String[] getTid() {
		return tid;
	}
	public void setTid(String[] tid) {
		this.tid = tid;
	}
	public String[] getScore() {
		return score;
	}
	public void setScore(String[] score) {
		this.score = score;
	}
}
